export function getUrlPara(url: string): Record<string, string> {
  if (!url || url.length === 0) {
    return {};
  }
  const queryIndex = url.indexOf('?');
  if (queryIndex === -1) return {};
  const paraString = url.substring(queryIndex + 1).split('&');
  const paraObj: Record<string, string> = {};
  for (let i = 0; i < paraString.length; i++) {
    const kv = paraString[i];
    const eq = kv.indexOf('=');
    if (eq === -1) continue;
    paraObj[kv.substring(0, eq)] = kv.substring(eq + 1);
  }
  return paraObj;
}

export function getItemIdFromAliurl(aliUrl: string): string {
  let aliUrlHead = 'http://detail.1688.com/offer/';
  let pos = aliUrl.indexOf(aliUrlHead);
  if (pos === -1) {
    aliUrlHead = 'https://detail.1688.com/offer/';
    pos = aliUrl.indexOf(aliUrlHead);
  }
  if (pos !== -1) {
    aliUrl = aliUrl.replace(aliUrlHead, '');
  } else {
    return '';
  }
  pos = aliUrl.indexOf('.htm');
  if (pos !== -1) {
    aliUrl = aliUrl.substring(0, pos);
    return aliUrl;
  }
  return '';
}

export function chunkArray<T>(array: T[], chunkSize: number): T[][] {
  const result: T[][] = [];
  for (let i = 0; i < array.length; i += chunkSize) {
    result.push(array.slice(i, i + chunkSize));
  }
  return result;
}


